﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
using System.IO;
using System.Runtime.Serialization.Formatters;
using System.Collections;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;

namespace FiveStone
{
    public partial class Form1 : Form
    {
        //public bool first=true;
        public Form1()
        {
            InitializeComponent();
        }

        public Boards bd;

        private void Form1_Load(object sender, EventArgs e)
        {            
            RegisterListenChannel();
        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            
        }

        private void Form1_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.X < 680 && e.Y < 680)
            {
                int m = (int)(e.X / 42);
                int n = (int)(e.Y / 42);
                if (m < 0){ m = 0; }
                if (n < 0){ n = 0; }
                if (m > 14){ m = 14; }
                if (n > 14){ n = 14; }
                label1.Text = "X：" + m.ToString() + "  Y：" + n.ToString();
                toolStripStatusLabel1.Text = "落子点：X " + m.ToString() + " Y " + n.ToString() + "";
            }
        }

        private void Form1_MouseDown(object sender, MouseEventArgs e)
        {
            bd.PersonPut(e.X, e.Y);

            if (historylist.Items.Count == 0)
            {
                historylist.Items.Add(bd.game.st.GetPerson());
                historylist.Items.Add(bd.game.st.GetPC());
            }
            else
            {
                if (historylist.Items[historylist.Items.Count-1].ToString() != bd.game.st.GetPC() && historylist.Items[historylist.Items.Count - 2].ToString() != bd.game.st.GetPerson())
                {
                    historylist.Items.Add(bd.game.st.GetPerson());
                    historylist.Items.Add(bd.game.st.GetPC());
                }
            }
        }                

        private void 玩家先ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (!玩家先ToolStripMenuItem.Checked)
            {
                historylist.Items.Clear();
                bd.Start(true);//玩家先                
                玩家先ToolStripMenuItem.Checked = true;
                电脑先ToolStripMenuItem.Checked = false;
                black.Text = "玩家";
                white.Text = "计算机";
                label2.Text = "1";
            }
        }

        private void 电脑先ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (!电脑先ToolStripMenuItem.Checked)
            {
                historylist.Items.Clear();
                bd.Start(false); //电脑先
                historylist.Items.Add(bd.game.st.GetPC());                
                电脑先ToolStripMenuItem.Checked = true;
                玩家先ToolStripMenuItem.Checked = false;
                black.Text = "计算机";
                white.Text = "玩家";
                label2.Text = "0";
            }
        }

        private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void 单机ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            bd = new Boards(this.CreateGraphics(),new Game());
            bd.ClearBoards();            
            bd.DrawBoard();            
            if (label2.Text == "0")
            {
                bd.Start(false);//电脑先
                historylist.Items.Add(bd.game.st.GetPC());
            }
            else
            {
                bd.Start(true);//玩家先
            }
            historylist.Items.Clear();
        }

        /// <summary>
        /// 创建主机
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void 联机ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Online onlineFrm = new Online();
            onlineFrm.ShowDialog();
        }

        /// <summary>
        /// 加入游戏
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void 加入游戏ToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void RegisterListenChannel()
        {
            // 通过编程设置反序列的级别
            BinaryServerFormatterSinkProvider provider = new BinaryServerFormatterSinkProvider();
            provider.TypeFilterLevel = TypeFilterLevel.Full;
            // 设置通道属性
            IDictionary props = new Hashtable();
            props["port"] = 0;
            //注册TCP通道，用于连接自己或者他人的服务器。
            TcpChannel chn = new TcpChannel(props, null, provider);
            ChannelServices.RegisterChannel(chn);
        }
        
    }
}
